<?php
include("config/session.php");
include("./config/config.inc.php");
include("./config/QuickMySQL.php");

class CSendEMailLog extends PageBase
{
	/* 加载页面 */
	function OnPageLoad(){
		include("./class/template.inc");
		
		$tpl = new Template("./ui/");
		
		// 显示发送邮件界面
		$tpl->set_file("main", "sendemail-log.html");
		$tpl->pparse("mains", "main");
	}
	/*格式化字符串*/
	function FormatTo($to){
		$to = str_replace("t_user_vm", "防毒墙用户", $to);
		$to = str_replace("t_user_qq", "QQ用户", $to);
		return $to;
	}
	/* 处理请求 */
	function OnGetData(){
		$ret = array("total" => 0, "rows" => array());
		$cnn = new QuickMySQL();
		if( $cnn->mysql ){
			/*连接数据库成功，查询相关数据*/
			$page = (int)$_POST["page"] - 1;
			$pagesize = (int)$_POST["rows"];
			if( $page < 0 ) $page = 0;
			if( $pagesize < 1 ) $pagesize = 1;
			$start = $page * $pagesize;
			/* 构造查询语句 */
			$sql = "select id, sm_to, sm_subject, success, count_s"
				. " , count_f, first_t, last_t"
				. " from t_sendmail_queue order by id desc limit {$start},{$pagesize}";
			if( $res = mysql_query($sql, $cnn->mysql) ){
				while( $row = mysql_fetch_row($res) ){
					array_push($ret["rows"], array(
								"id" => $row[0]
								, "to" => $this->FormatTo($row[1])
								, "subject" => $row[2]
								, "success" => $row[3]
								, "count_s" => $row[4]
								, "count_f" => $row[5]
								, "first_dt" => $row[6]
								, "last_dt" => $row[7]			
								));	
				}
				mysql_free_result($res);
			}
			/*查询总个数*/
			$ret["total"] = $cnn->getTableCount("t_sendmail_queue");
		}
		
		$ret_json = json_encode($ret);
		echo $ret_json;
	}
	/*查看邮件内容*/
	function OnLookBody(){
		if( !isset($_GET['id']) )
			return;
		$sql = "select sm_body from t_sendmail_queue where id={$_GET['id']}";
		$cnn = new QuickMySQL();
		if( !$cnn->mysql )
			return;
		$res = $cnn->Query($sql);
		if( $row = mysql_fetch_row($res) ){
			echo $row[0];
		}
		mysql_free_result($res);
	}
}

$cc = new CSendEMailLog();
?>